﻿using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;

namespace ApiLib.RCL.Utils;

public static class RazorUtil
{
    /// <summary>
    /// 显示弹窗，默认事件：[Parameter] public Func&lt;bool, Task>? OnClose { get; set; }
    /// </summary>
    /// <param name="dialog"></param>
    /// <param name="title">标题</param>
    /// <param name="size">大小，默认Medium</param>
    /// <param name="param">参数</param>
    /// <param name="successFunc">成功的回调</param>
    /// <typeparam name="T"></typeparam>
    public static async Task ShowDialog<T>(this DialogService dialog, string title, Size size = Size.Medium,
        Dictionary<string, object?>? param = null,
        Func<Task>? successFunc = null)
        where T : IComponent
    {
        var opt = new DialogOption()
        {
            Title = title,
            Size = size,
            ShowFooter = false
        };
        param ??= new Dictionary<string, object?>();
        param["OnClose"] = new Func<bool, Task>(async flag =>
        {
            await opt.CloseDialogAsync();
            if (flag && successFunc != null)
            {
                await successFunc();
            }
        });
        opt.BodyTemplate = BootstrapDynamicComponent.CreateComponent<T>(param).Render();
        await dialog.Show(opt);
    }

    #region Notify

    /// <summary>
    /// 警告通知
    /// </summary>
    /// <param name="messageService"></param>
    /// <param name="content"></param>
    public static async Task MessageWarning(this MessageService messageService, string content)
    {
        await messageService.ToMessage(content, Color.Warning);
    }

    /// <summary>
    /// 危险通知
    /// </summary>
    /// <param name="messageService"></param>
    /// <param name="content"></param>
    public static async Task MessageDanger(this MessageService messageService, string content)
    {
        await messageService.ToMessage(content, Color.Danger);
    }

    /// <summary>
    /// 成功通知
    /// </summary>
    /// <param name="messageService"></param>
    /// <param name="content"></param>
    public static async Task MessageSuccess(this MessageService messageService, string content)
    {
        await messageService.ToMessage(content, Color.Success);
    }

    /// <summary>
    /// 通知
    /// </summary>
    /// <param name="messageService"></param>
    /// <param name="content"></param>
    /// <param name="color"></param>
    public static async Task ToMessage(this MessageService messageService, string content, Color color)
    {
        await messageService.Show(new MessageOption()
        {
            Content = content,
            Icon = "fa fa-info-circle",
            Color = color
        });
    }

    /// <summary>
    /// 弹窗确认
    /// </summary>
    /// <param name="swalService"></param>
    /// <param name="title"></param>
    /// <param name="content"></param>
    /// <param name="autoClose"></param>
    public static async Task SweetWarning(this SwalService swalService, string title = "", string content = "",
        bool autoClose = true)
    {
        await swalService.Show(new SwalOption()
        {
            Category = SwalCategory.Warning,
            Title = title,
            Content = content,
            IsAutoHide = autoClose
        });
    }

    /// <summary>
    /// Sweet
    /// </summary>
    /// <param name="swalService"></param>
    /// <param name="title"></param>
    /// <param name="content"></param>
    /// <param name="autoClose"></param>
    public static async Task SweetSuccess(this SwalService swalService, string title = "", string content = "",
        bool autoClose = true)
    {
        await swalService.Show(new SwalOption()
        {
            Category = SwalCategory.Success,
            Title = title,
            Content = content,
            IsAutoHide = autoClose
        });
    }

    /// <summary>
    /// Sweet
    /// </summary>
    /// <param name="swalService"></param>
    /// <param name="option"></param>
    /// <returns></returns>
    public static async Task<bool> SweetModal(this SwalService swalService, SwalOption option)
    {
        return await swalService.ShowModal(option);
    }

    /// <summary>
    /// Sweet
    /// </summary>
    /// <param name="swalService"></param>
    /// <param name="title"></param>
    /// <param name="content"></param>
    /// <param name="autoClose"></param>
    /// <param name="category"></param>
    /// <returns></returns>
    public static async Task<bool> SweetModal(this SwalService swalService, string title = "", string content = "",
        bool autoClose = true,
        SwalCategory category = SwalCategory.Warning)
    {
        return await swalService.ShowModal(new SwalOption()
        {
            Category = category,
            Title = title,
            Content = content,
            IsAutoHide = autoClose
        });
    }

    #endregion
}